package com.mdd.admin.service.generate.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mdd.admin.service.generate.GenerateService;
import com.mdd.common.constant.GlobalConstant;
import com.mdd.common.entity.goods.Goods;
import com.mdd.common.entity.goods.GoodsComment;
import com.mdd.common.entity.user.User;
import com.mdd.common.mapper.comment.GoodsCommentMapper;
import com.mdd.common.mapper.generate.GenerateMapper;
import com.mdd.common.mapper.goods.GoodsMapper;
import com.mdd.common.mapper.user.UserMapper;
import com.mdd.common.utils.IpUtil;
import com.mdd.common.utils.TimeUtil;
import com.mdd.common.utils.ToolsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
@Slf4j
public class IGenerateServiceImpl implements GenerateService {

	@Resource
	private UserMapper userMapper;

	@Resource
	private GenerateMapper generateMapper;

	@Resource
	private GoodsMapper goodsMapper;

	@Resource
	private GoodsCommentMapper goodsCommentMapper;

	@Value("${wx.app.avatarUrl}")
	private String avatarUrl;


	/**
	 *
	 */
	@Override
	public void generateUser() {
		Integer num = 0;
		while (num <= 100) {
			Integer sn;
			while (true) {
				sn = Integer.parseInt(ToolsUtil.randomInt(8));
				User snModel = userMapper.selectOne(new QueryWrapper<User>()
						.select("id,sn,username")
						.eq("sn", sn)
						.last("limit 1"));
				if (snModel == null) {
					break;
				}
			}
			String prefix = ToolsUtil.randomString(2);
			String suffix = ToolsUtil.randomString(2);
			String nickName = prefix + "***" + suffix;

			User model = new User();

			model.setSn(sn);
			model.setAvatar(avatarUrl);
			model.setNickname(nickName);
			model.setUsername("u" + sn);
			model.setSex(0);
			model.setChannel(1);
			model.setIsNew(1);
			model.setLastLoginIp(IpUtil.getHostIp());
			model.setLastLoginTime(System.currentTimeMillis() / 1000);
			model.setCreateTime(System.currentTimeMillis() / 1000);
			model.setUpdateTime(System.currentTimeMillis() / 1000);
			userMapper.insert(model);
			num++;
		}
	}


	List<Integer> size = new ArrayList<>();

	List<Double> list = Arrays.asList(0.15, 0.16, 0.17, 0.18, 0.19, 0.2);

	/**
	 *
	 */
	@Override
	public void generateComment() {
		// 查询出所有不存在的用户
		List<User> users = userMapper.selectList(new LambdaQueryWrapper<User>().like(User::getNickname, "***"));

		List<Goods> goods = goodsMapper.selectList(new LambdaQueryWrapper<Goods>().eq(Goods::getIsDelete, GlobalConstant.NOT_DELETE));

		goods.forEach(good -> {
			// 获取评论内容
			String jsonComment = generateMapper.selectCommentByGoodsId(Math.toIntExact(good.getId()));
			if(jsonComment == null) {
				return;
			}
			JSONObject jsonObject = JSONUtil.parseObj(jsonComment);
			JSONArray jsonArray = jsonObject.getJSONArray("comments");
			ArrayList<Integer> arrayList = new ArrayList<>();
			Integer orderNum = good.getOrderNum();
			Integer t = (int)(orderNum * list.get(RandomUtil.randomInt(0, 5)));

			// 创建t条评论
			for (int i = 0; i < t;) {
				int i1 = RandomUtil.randomInt(0, users.size() - 1);
				User user = users.get(i1);
				GoodsComment model = new GoodsComment();
				model.setGoodsId(good.getId());
				model.setUserId(Long.valueOf(user.getId()));
				model.setOrderGoodsId(0L);
				model.setServiceComment(5);
				String comment = jsonArray.getStr(RandomUtil.randomInt(0, jsonArray.size() - 1));
				model.setComment(comment);
				model.setCommentLevel(1);
				int f = RandomUtil.randomInt(0, 31783107);
				long l1 = TimeUtil.timestamp() - f;
				model.setCreateTime(l1);
				model.setUpdateTime(l1);
				goodsCommentMapper.insert(model);
				i++;
			}
		});



	}
}
